package com.want.buss.interfaces.aop;


import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.want.buss.interfaces.po.WsdlStatus;
import com.want.buss.interfaces.service.IWsdlStatusService;

@Aspect
@Component
public class OperLogInterceptor {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    IWsdlStatusService wsdlStatusService;
    
	protected WsdlStatus webServiceStatus = null;
	
    @Around("within(com.want.buss.interfaces.service..*) && @annotation(operLog)")
    public Object doAroundMethod(ProceedingJoinPoint pjd,OperLog operLog) throws Throwable {
    	String className = pjd.getTarget().getClass().getName();
    	String method    = pjd.getSignature().getName();
    	Object[] params  = pjd.getArgs();
    	long startTime = System.currentTimeMillis();
        logger.info("执行开始"+className+"."+method+"  参数为："+Arrays.toString(params));
        long sid = Calendar.getInstance().getTimeInMillis();
        webServiceStatus = new WsdlStatus(sid,operLog.operName(),new Date(),"1","");
        Object result  = null;
        try {
        	 result  = pjd.proceed();
		} catch (Exception e) {
			webServiceStatus.setStatus("0");
			webServiceStatus.setReason(e.getMessage());
			logger.error("执行异常"+className+"."+method+" 异常信息为"+e);
		} finally {
			webServiceStatus.setEndDate(new Date());
			wsdlStatusService.insert(webServiceStatus);
		}
        long endTime=System.currentTimeMillis();
        float excTime=(float)(endTime-startTime)/1000;
        logger.info("结束执行"+className+"."+method+"  返回值为："+result + " 耗费"+excTime+"s");
        return result;
    } 
    

}
